package com.cl.ks.controller;


import com.cl.ks.repository.KsFlowTaskRepository;
import com.cl.ks.schedule.KsFlowTaskStarter;
import com.cl.ks.utils.CommonResult;
import com.cl.schedule.controller.ScheduleController;
import com.cl.ks.entity.KsFlowTask;
import com.cl.ks.service.KsFlowTaskService;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.Optional;

@RestController
@RequestMapping("KsFlowTask")
public class KsFlowTaskController extends ScheduleController<KsFlowTask> {

    private final KsFlowTaskStarter ksFlowTaskStarter;
    private final KsFlowTaskRepository ksFlowTaskRepository;

    public KsFlowTaskController(KsFlowTaskService service, KsFlowTaskStarter ksFlowTaskStarter,
                                KsFlowTaskRepository ksFlowTaskRepository) {
        super(service);
        this.ksFlowTaskStarter = ksFlowTaskStarter;
        this.ksFlowTaskRepository = ksFlowTaskRepository;
    }

    @GetMapping("/start")
    public CommonResult start(@RequestParam(name = "ksFlowTaskId") Integer ksFlowTaskId) {
        Optional<KsFlowTask> ksFlowTaskOptional = ksFlowTaskRepository.findById(ksFlowTaskId);
        if (ksFlowTaskOptional.isPresent()) {
            KsFlowTask ksFlowTask = ksFlowTaskOptional.get();
            ksFlowTask.setTaskFlag(false);
            ksFlowTaskStarter.start(ksFlowTask);
            return CommonResult.success("执行成功");
        } else {
            return CommonResult.failed("id不存在");
        }
    }

}
